package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.service.PatientService;

public class UploadPatientImageController extends UploadImageController {

	private PatientService patientService;

	@SuppressWarnings("deprecation")
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		Patient patient = (Patient) command;

		processUpload(request, response, command, errors, patient.getPhoto());

		patientService.save(patient);

		return new ModelAndView(getSuccessView(), "patientId", patient.getId());
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return patientService
				.load(request.getParameter("patientId").toString());
	}

	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request,
			Object command, Errors errors) throws Exception {
		Map<String, Object> mapData = new HashMap<String, Object>();

		mapData.put("patient", patientService.load(request.getParameter(
				"patientId").toString()));
		return mapData;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

}